package com.tulele.vtool.service.impl;

import com.tulele.vtool.mapper.SysGzhMapper;
import com.tulele.vtool.model.*;
import com.tulele.vtool.model.combinedEntity.SlideshowCombinedEntity;
import com.tulele.vtool.service.SysGzhService;
import com.tulele.vtool.utils.RedisCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class SysGzhServiceImpl implements SysGzhService {

    @Autowired
    private SysGzhMapper sysGzhMapper;

    @Autowired
    private RedisCacheUtil redisCacheUtil;

    @Override
    public SysGzh selectCacheLinkByGzhCode(String gzhCode) {
        //return sysGzhMapper.selectCacheLinkByGzhCode(gzhCode);
        PlatformSysInfo platformSysInfo=getPlatformSysInfo(gzhCode);
        return platformSysInfo.getSysGzh();
    }

    @Override
    public GzhBrandMap getGzhBrandMap(Integer brandId){
        return sysGzhMapper.getGzhBrandMap(brandId);
    }

    @Override
    public List<Integer> getGzhBrandMapByGzh(String gzhCode){
        return sysGzhMapper.getGzhBrandMapByGzh(gzhCode);
    }



    @Override
    public GzhUserMap getGzhUserMap(Integer sUserId, String tGzhCode){
        return sysGzhMapper.getGzhUserMap(sUserId,tGzhCode);
    }

    @Override
    public GzhUserMap checkGzhUserMap(Integer sUserId, Integer tUserId){
        return sysGzhMapper.checkGzhUserMap(sUserId,tUserId);
    }

    @Override
    public int addGzhUserMap(GzhUserMap gzhUserMap){
        return sysGzhMapper.addGzhUserMap(gzhUserMap);
    }

    @Override
    public SysRealmMessage getRealmByGzhCode(String gzhCode){

        String BeBornKey = "BeBorn:" + gzhCode;

        SysRealmMessage sysRealmMessage = null;
        Object beBorn = redisCacheUtil.getCacheObject(BeBornKey);
        if (beBorn != null) {
            sysRealmMessage = (SysRealmMessage) beBorn;
        } else {
            List<SysRealmMessage> sysRealmMessageList= sysGzhMapper.getRealmByGzhCode(gzhCode);
            if(sysRealmMessageList!=null && sysRealmMessageList.size()>0) {
                sysRealmMessage = sysRealmMessageList.get(0);
                redisCacheUtil.setCacheObject(BeBornKey, sysRealmMessage, 2592000);
            }
        }

        return sysRealmMessage;
    }

    @Override
    public List<SysRealmMessage> getRealmByGzhCodeList(String gzhCode){
        return  sysGzhMapper.getRealmByGzhCode(gzhCode);
    }

    @Override
    public SysRealmMessage getGzhCodeRealmByVipStatus(String gzhCode) {
        String cacheKey = "GzhCodeRealm:" + gzhCode;
        Object sysRealmMessageObj = redisCacheUtil.getCacheObjectMaster(cacheKey);

        SysRealmMessage sysRealmMessage = new SysRealmMessage();
        if (Objects.isNull(sysRealmMessageObj)) {
            sysRealmMessage = sysGzhMapper.getGzhCodeRealmByVipStatus(gzhCode);

            redisCacheUtil.setCacheObjectMaster(cacheKey, sysRealmMessage, 2592000);//(30天)
        } else {
            sysRealmMessage = (SysRealmMessage) sysRealmMessageObj;
        }
        return sysRealmMessage;
    }

    @Override
    public List<SlideshowCombinedEntity> getGzhSlideshow(String gzhCode) {
        return sysGzhMapper.getGzhSlideshow(gzhCode);
    }


    @Override
    public PlatformSysInfo getPlatformSysInfo(String gzhCode) {

        String PlatformKey = "PlatformSysInfo:" + gzhCode;

        PlatformSysInfo platformSysInfo = null;
        Object platformSysInfoObj = redisCacheUtil.getCacheObject(PlatformKey);
        if (platformSysInfoObj != null) {
            platformSysInfo = (PlatformSysInfo) platformSysInfoObj;
        } else {
            SysGzh sysGzh = sysGzhMapper.selectCacheLinkByGzhCode(gzhCode);
            platformSysInfo=bindPlatformSysInfo(sysGzh);
            redisCacheUtil.setCacheObject(PlatformKey, platformSysInfo, 2592000);
        }

        return platformSysInfo;
    }

    @Override
    public PlatformSysInfo getPlatformSysInfoByAppId(String appId) {

        String PlatformKey = "PlatformSysInfo:" + appId;

        PlatformSysInfo platformSysInfo = null;
        Object platformSysInfoObj = redisCacheUtil.getCacheObject(PlatformKey);
        if (platformSysInfoObj != null) {
            platformSysInfo = (PlatformSysInfo) platformSysInfoObj;
        } else {
            SysGzh sysGzh = sysGzhMapper.selectCacheLinkByAppId(appId);
            platformSysInfo=bindPlatformSysInfo(sysGzh);
            redisCacheUtil.setCacheObject(PlatformKey, platformSysInfo, 2592000);
        }

        return platformSysInfo;
    }

    private PlatformSysInfo bindPlatformSysInfo(SysGzh sysGzh){
        PlatformSysInfo platformSysInfo=new PlatformSysInfo();

        platformSysInfo.setFrontDomain(sysGzh.getRealmName());//会员域名

        platformSysInfo.setWxArticleSpiderUrl("http://172.18.165.241/spiderArticleInfo/");

        platformSysInfo.setArticleDetailUrl(sysGzh.getRealmMessage()+"pages/article/detail.html?articleId=");//文章详情链接

        platformSysInfo.setArticleListMenuUrl(sysGzh.getRealmResource()+"list.html");//文章库链接

        platformSysInfo.setClockMenuUrl(sysGzh.getRealmMessage()+"index.html");//打卡链接

//        platformSysInfo.setCustomerMenuUrl(sysGzh.getRealmName()+"pages/customer/index.html");//客户页链接
        platformSysInfo.setCustomerMenuUrl(sysGzh.getRealmName()+"pages/Newpage/Customerpage.html");//客户页链接

        platformSysInfo.setOpenMemberMenuUrl(sysGzh.getRealmName()+"pages/setting/member.html");//开通会员页

//        platformSysInfo.setSetUpMenuUrl(sysGzh.getRealmName()+"pages/setting/index.html");//设置名片页
        platformSysInfo.setSetUpMenuUrl(sysGzh.getRealmName()+"pages/Newpage/Myhome.html");//设置名片页

        platformSysInfo.setMicroStationMenuUrl(sysGzh.getRealmName()+"pages/home/index.html");//我的微站页

        platformSysInfo.setGuideForUseMenuUrl("");

//        platformSysInfo.setCooperationUseMenuEventKey("V1001_GENERATE_PROMOTE");//“邀请有奖”关键字
        platformSysInfo.setCooperationUseMenuEventKey(sysGzh.getRealmName()+"pages/Newpage/invitation.html");//“邀请有奖”页

        platformSysInfo.setSpokesManMenuUrl("");

        platformSysInfo.setShareDomainUrl(sysGzh.getRealmMessage());//分享出去 的域名

        platformSysInfo.setGetNetworkMenuEventKey("V1002_GET_NETWORK");

        platformSysInfo.setTutorialUrl(sysGzh.getTutorialUrl());//使用指南

        platformSysInfo.setCooperationUrl(sysGzh.getCooperationUrl());//合作推广

        platformSysInfo.setMyBrandsUrl(sysGzh.getRealmResource()+ "list.html?tab=brand");//我的品牌

        platformSysInfo.setTaskUrl(sysGzh.getTaskUrl());//消息项目的URL及端口

        platformSysInfo.setClassManagementUseMenuEventKey("V1003_CLASS_MANAGEMENT");//班级管理(小铺)

        platformSysInfo.setSettlementUrl(sysGzh.getRealmResource()+ "pages/article/detail.html?articleId=2246806&t=2");//结算中心(小铺)

        platformSysInfo.setVideoListMenuUrl(sysGzh.getRealmResource()+"list.html?tab=video");//获客视频

        platformSysInfo.setSzjiMenuUrl("https://xls-act.sibumbg.cn/companyProfile/?from=groupmessage&isappinstalled=0");//思埠简介(有鱼)

        platformSysInfo.setCanvassBusinessOrdersMenuUrl(sysGzh.getRealmMessage()+"pages/officialAccounts/activityIntroduce.html");//一键招商

        platformSysInfo.setHeadSculptureMenuUrl(sysGzh.getRealmMessage()+"pages/setting/makeHeadPortrait.html?v=96");//头像菜单

        platformSysInfo.setConnectionPlazaUrl(sysGzh.getRealmName() +"pages/officialAccounts/connectionPlaza.html");//人脉广场

        platformSysInfo.setWxShareImgPath("/mnt/share/"+sysGzh.getOpenGroupCode()+"/");
        platformSysInfo.setPayCallbackUrl(sysGzh.getServerUrl()+"wechat/wxPayCallback/"+sysGzh.getGzhCode());
        platformSysInfo.setAuthorizeRedirectUrl(sysGzh.getServerUrl()+"wechat/jumpToRedirectUrl?");
        platformSysInfo.setGenerateMessagePicUrl("https://zktuistatic.oss-cn-shenzhen.aliyuncs.com/wxTouXiang/guoqing/gq.png");
        platformSysInfo.setPayCallbackUrlByBusiness(sysGzh.getServerUrl()+"wechat/wxPayCallbackByBusiness/"+sysGzh.getGzhCode());

        platformSysInfo.setDataCenterUrl(sysGzh.getRealmName() + "pages/Newpage/Customerassetdata.html");// 数据表单

        platformSysInfo.setUserAdvertUrl(sysGzh.getRealmName() + "pages/Newpage/Bottomadlist.html");// 我的广告

        platformSysInfo.setOperatingGuide(sysGzh.getRealmName() + "pages/article/faq.html?id=10");// 使用指南

        platformSysInfo.setSysGzh(sysGzh);

        return platformSysInfo;
    }
}
